package com.cuitrip.business.home.travel.ui;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.business.home.travel.model.PopupFilterCategory;
import com.cuitrip.business.home.travel.model.PopupFilterItem;
import com.cuitrip.component.flowlayout.TagView;
import com.cuitrip.component.recyclerview.ItemClickSupport;
import com.cuitrip.service.R;
import com.cuitrip.util.FlowLayout;
import com.lab.component.list.CTRecyclerView;
import com.lab.component.list.simple.CTSimpleRecyclerAdapter;
import com.lab.component.list.simple.CTSimpleViewHolder;

import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by ak
 */
public class PopupFilterViewHolder<T extends PopupFilterCategory<F>, F extends PopupFilterItem> {

    public static final int CATEGORY_TYPE = 0x01;
    public static final int DESTINATION_TYPE = 0x02;
    CTRecyclerView mRecyclerView;
    private Context mContext;
    private List<T> mPopupMenuItemList;
    private PopupMenuCallback<T> mPopupMenuCallback;
    private View mRootView;
    private int type = CATEGORY_TYPE;

    private CTSimpleRecyclerAdapter mAdapter;


    public PopupFilterViewHolder(Context context, int type, List<T> popupMenuItemList, View rootView, PopupMenuCallback popupMenuCallback) {
        mContext = context;
        this.mPopupMenuCallback = popupMenuCallback;
        this.mPopupMenuItemList = popupMenuItemList;
        this.type = type;
        this.mRootView = rootView;
        internalInit();
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    private void internalInit() {
        if (type == CATEGORY_TYPE) {
            mRecyclerView = ButterKnife.findById(mRootView, R.id.ct_filter_recycler_view);
        } else {
            mRecyclerView = ButterKnife.findById(mRootView, R.id.ct_destination_recycler_view);
        }
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new CTSimpleRecyclerAdapter<T>(mContext, mPopupMenuItemList) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.ct_layout_filter_item;
            }

            @Override
            public void onNewViewHolder(CTSimpleViewHolder viewHolder, ViewGroup parent, int viewType) {

            }

            @Override
            public void bindData(CTSimpleViewHolder holder, final int position, final T item) {
                FlowLayout flowLayout = holder.getView(R.id.flow_layout);
                TextView categoryText = holder.getView(R.id.tv_category);
                categoryText.setText(item.getCategoryTitle());

                List<F> filterItemList = item.getList();
                for (int i = 0; i < filterItemList.size(); i++) {
                    final PopupFilterItem popupFilterItem = filterItemList.get(i);
                    if (popupFilterItem.isShow()) {
                        final TagView textView = new TagView(mContext
                                , popupFilterItem.getKey());
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
                        textView.setBackgroundResource(R.drawable.select_white_corner);
                        textView.setSelected(popupFilterItem.isSelected());
                        textView.setTextColor(mContext.getResources().getColorStateList(R.color.ct_white_qiaomai));
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                popupFilterItem.setSelected(!popupFilterItem.isSelected());
                                textView.setSelected(popupFilterItem.isSelected());
                            }
                        });

                        flowLayout.addView(textView);
                    }
                }


            }
        };
        mRecyclerView.setAdapter(mAdapter);
    }


    public void notifyDataChanged() {
        mAdapter.notifyDataSetChanged();
    }

    public void show() {
        if (mRootView.getVisibility() != View.VISIBLE) {
            mRootView.setVisibility(View.VISIBLE);
        }
        if (mPopupMenuCallback != null) {
            mPopupMenuCallback.onPopupMenuStateChanged(true);
        }
    }

    public void hide() {
        if (mRootView.getVisibility() != View.GONE) {
            mRootView.setVisibility(View.GONE);
        }
        if (mPopupMenuCallback != null) {
            mPopupMenuCallback.onPopupMenuStateChanged(false);
        }
    }

    public boolean isShow() {
        return mRootView.getVisibility() == View.VISIBLE;
    }

    public void setPopupMenuItemList(List<T> popupMenuItemList) {
        this.mPopupMenuItemList.clear();
        this.mPopupMenuItemList.addAll(popupMenuItemList);
        mAdapter.notifyDataSetChanged();
    }

    private void setPopupMenuCallback(PopupMenuCallback callback) {
        mPopupMenuCallback = callback;
    }

    public void destroy() {
        ItemClickSupport.removeFrom(mRecyclerView.getRecyclerView());
    }

    public interface PopupMenuCallback<T> {
        boolean onPopupMenuItemClick(int position, T item);

        void onPopupMenuStateChanged(boolean isShow);
    }

}
